//app.js
var user = require("./utils/login.js")
App({
  globalData: {
    userId: "",
    baseUrlImg: "http://101.200.46.8:25603/HD/",
    baseUrlApiTest: "http://101.200.46.8:25601/API_ROP/",
    isLogin: false,
    isVIP: false, // 判断用户是否是VIP
    memberId: '', // 用户会员id
  },
  onLaunch: function () {
    let res1 = JSON.parse(wx.getStorageSync('openid') || '{}');
    this.globalData.userId = res1.openId;
    this.globalData.memberId = res1.memberId;
    // user.getLogin().then(res => {
    //   console.log(res)
    //   if (res.memberId != '' && res.memberId != undefined) {
    //     this.globalData.isVIP = true
    //     this.globalData.memberId = res.memberId
    //   }
    //   user.getUser().then(res => {
    //     console.log(JSON.stringify(res))
    //   })
    // })
  },
  onShow() {
    user.getLogin().then(res => {
      user.getUser().then(res => {
        console.log(JSON.stringify(res))
      })
      user.checkLogin().then(res => {
        console.log('app login')
        console.log(res)
        this.globalData.userInfo = wx.getStorageSync('userInfo');
        this.globalData.isLogin = true
      }).catch(() => {
        // user.getLogin().then(res => {
        // 检查登陆态
        user.checkSession().then(res => {
          console.log(res)
        }).catch(res => {
          console.log(res)

          // })
        });
      })

      user.checkLogin().then(res => {
        console.log(res)
        this.globalData.userInfo = wx.getStorageSync('userInfo');
        this.globalData.isLogin = true
      }).catch(() => {
        // user.getLogin().then(res => {

        // })
      });
    })
  },
  // 本地添加播放历史
  recordHistory(data){
    let historyData = wx.getStorageSync('historyData') || {};
    let historyList = historyData.dataList || [];
    let obj = data || {};
    // 如果有重复的就删掉
    for(var i = historyList.length - 1; i >= 0; i--) {
      if (historyList[i].code == obj.code) {
        historyList.splice(i, 1);
      }
    }
    historyList.slice(0, 9);//截取前9个
    historyList.unshift(obj);//把新增的数据放在第一个
    console.log(historyList);
    historyData.dataList = historyList;
    wx.setStorage({
      key: "historyData",
      data: historyData
    })
  },
  // 删除本地播放历史
  delRecordHistory(code) {
    let historyData = wx.getStorageSync('historyData') || {};
    let historyList = historyData.dataList || [];
    // 如果为空，全部删除
    if (!code){
      historyList = [];
    }else{
      // 如果有相同就删掉
      for (var i = historyList.length - 1; i >= 0; i--) {
        if (historyList[i].code == code) {
          historyList.splice(i, 1);
        }
      }
      historyList.slice(0, 10);//截取前10个
    }
    console.log(historyList);
    historyData.dataList = historyList;
    wx.setStorage({
      key: "historyData",
      data: historyData
    })
  },
  /*// 本地添加收藏记录
  recordCollection(data) {
    let collectionData = wx.getStorageSync('collectionData') || {};
    let collectionList = collectionData.dataList || [];
    let obj = data || {};
    // 如果有重复的就删掉
    for (var i = collectionList.length - 1; i >= 0; i--) {
      if (collectionList[i].code == obj.code) {
        collectionList.splice(i, 1);
      }
    }
    collectionList.slice(0, 9);//截取前9个
    collectionList.unshift(obj);//把新增的数据放在第一个
    console.log(collectionList);
    collectionData.dataList = collectionList;
    wx.setStorage({
      key: "collectionData",
      data: collectionData
    })
  },
  // 删除本地收藏记录
  delRecordCollection(code) {
    let collectionData = wx.getStorageSync('collectionData') || {};
    let collectionList = collectionData.dataList || [];
    // 如果有重复的就删掉
    for (var i = collectionList.length - 1; i >= 0; i--) {
      if (collectionList[i].code == code) {
        collectionList.splice(i, 1);
      }
    }
    collectionList.slice(0, 10);//截取前10个
    console.log(collectionList);
    collectionData.dataList = collectionList;
    wx.setStorage({
      key: "collectionData",
      data: collectionData
    })
  },*/
})